CC=gcc
#CFLAGS=-Wall -fpic
CFLAGS=-Wall
LIBPATH=../..
INCLS=-I$(LIBPATH)/include
OS=$(shell uname)
CPU=$(shell uname -m)

ifeq ($(LIBTYPE), debug)
    CFLAGS+=-g
    LIB=libsoar_g
endif
ifeq ($(LIBTYPE), release)
    CFLAGS+=-O3
    LIB=libsoar
endif

OBJS+=cnfg.o datetime.o dbc.o encoding.o errcode.o ep.o http.o list.o log.o mail.o misc.o msgq.o netcm.o ossl.o para.o shm.o sig.o skt.o sock.o sockd.o thrd.o tree.o

ifeq ($(OS), Linux)
    option=$(shell if test -n "$$ORACLE_HOME" -a -d "$$ORACLE_HOME" -a -x "$$ORACLE_HOME/bin/proc"; then echo Y; else echo N; fi)
    ifeq ($(option), Y)
        NLS_LANG=American_America.UTF8
        OBJS+=oraproc.o oraproc_mt.o
        ORA_INCLS=-I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo
        ORAPROC_SYS_INCLS=sys_include=/usr/include sys_include=$(LIBPATH)/include sys_include=$(ORACLE_HOME)/precomp/public sys_include=$(ORACLE_HOME)/rdbms/public sys_include=$(ORACLE_HOME)/rdbms/demo
        release=$(shell if test -f /etc/redhat-release; then echo REDHAT; else echo UNKNOWN; fi)
        ifeq ($(release), REDHAT)
            ifeq ($(CPU), i686)
                cpu=i386
            endif
            ifeq ($(CPU), x86_64)
                cpu=x86_64
            endif
            ORAPROC_SYS_INCLS+=$(shell \
					cat /etc/redhat-release | awk '\
					{\
					if($$0~/CentOS release 4/)\
					{ print "sys_include=/usr/lib/gcc/$(cpu)-redhat-linux/3.4.3/include" }\
					else if($$0~/CentOS release 5/)\
					{ print "sys_include=/usr/lib/gcc/$(cpu)-redhat-linux/4.1.1/include" }\
					else if($$0~/Fedora release 9/)\
					{ print "sys_include=/usr/lib/gcc/$(cpu)-redhat-linux/4.3.0/include" }\
					else if($$0~/Fedora release 10/)\
					{ print "sys_include=/usr/lib/gcc/$(cpu)-redhat-linux/4.3.2/include" }\
					else if($$0~/Fedora release 11/)\
					{ print "sys_include=/usr/lib/gcc/$(cpu)-redhat-linux/4.4.0/include" }\
					}')
        endif
    endif

    option=$(shell if test -d /usr/local/mysql/include; then echo CUSTOM; elif test -d /usr/include/mysql; then echo SYSTEM; else echo NO; fi)
    ifeq ($(option), CUSTOM)
        OBJS+=mysqlc.o
        MYSQL_INCLS=-I/usr/local/mysql/include
    endif
    ifeq ($(option), SYSTEM)
        OBJS+=mysqlc.o
        MYSQL_INCLS=-I/usr/include/mysql
    endif

#option=$(shell if test -d /usr/local/pgsql/include; then echo CUSTOM; elif test -d /usr/include/pgsql; then echo SYSTEM; else echo NO; fi)
#ifeq ($(option), CUSTOM)
#    OBJS+=pgc.o
#    PGSQL_INCLS=-I/usr/local/pgsql/include
#endif
#ifeq ($(option), SYSTEM)
#    OBJS+=pgc.o
#    PGSQL_INCLS=-I/usr/include/pgsql
#endif

    option=$(shell if test -f /usr/local/include/sqlite3.h; then echo CUSTOM; elif test -f /usr/include/sqlite3.h; then echo SYSTEM; else echo NO; fi)
    ifeq ($(option), CUSTOM)
        OBJS+=sqlic.o
        SQLI_INCLS=-I/usr/local/include
    endif
    ifeq ($(option), SYSTEM)
        OBJS+=sqlic.o
    endif
endif

ifeq ($(OS), FreeBSD)
    option=$(shell if test -d /usr/local/include/mysql; then echo Y; else echo N; fi)
    ifeq ($(option), Y)
        OBJS+=mysqlc.o
        MYSQL_INCLS=-I/usr/local/include/mysql
    endif

    option=$(shell if test -f /usr/local/include/sqlite3.h; then echo Y; else echo N; fi)
    ifeq ($(option), Y)
        OBJS+=sqlic.o
        SQLI_INCLS=-I/usr/local/include
    endif
endif

.PHONY : all

all : $(OBJS)
#	$(CC) -shared -o ../../lib/$(LIB).so $^
#	chmod 644 ../../lib/$(LIB).so
	ar -crl ../../lib/$(LIB).a $^
	chmod 644 ../../lib/$(LIB).a

#orac.o : orac.c
#	$(CC) $(CFLAGS) $(INCLS) -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -c -o $@ $<

oraproc.o : oraproc.pc
	proc sqlcheck=syntax $(ORAPROC_SYS_INCLS) $<
	$(CC) $(CFLAGS) $(INCLS) $(ORA_INCLS) -c -o $@ $*.c
	rm -f $*.c $*.lis tp*

oraproc_mt.o : oraproc_mt.pc
	proc sqlcheck=syntax threads=yes $(ORAPROC_SYS_INCLS) $<
	$(CC) $(CFLAGS) $(INCLS) $(ORA_INCLS) -c -o $@ $*.c
	rm -f $*.c $*.lis tp*

mysqlc.o : mysqlc.c
	$(CC) $(CFLAGS) $(INCLS) $(MYSQL_INCLS) -c -o $@ $<

pgc.o : pgc.c
	$(CC) $(CFLAGS) $(INCLS) $(PGSQL_INCLS) -c -o $@ $<

sqlic.o : sqlic.c
	$(CC) $(CFLAGS) $(INCLS) $(SQLI_INCLS) -c -o $@ $<

%.o : %.c
	$(CC) $(CFLAGS) $(INCLS) -c -o $@ $<
